package excel

import (
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
	"grpc_tapd_s/model"
	"math/rand"
	"strconv"
	"time"
)

func area(categoryId int64) (areaName string) {
	switch categoryId {
	case 1166551862001000340:
		areaName = "房山区"
	case 1166551862001000345:
		areaName = "崇外街道"
	case 1166551862001000347:
		areaName = "平房乡"
	case 1166551862001000343:
		areaName = "和义街道"
	case 1166551862001000370:
		areaName = "门头沟"
	case 1166551862001000371:
		areaName = "大峪街道"
	case 1166551862001000377:
		areaName = "海淀市场局"
	case 1166551862001000341:
		areaName = "成寿寺街道"
	case 1166551862001000368:
		areaName = "西城区"
	case 1166551862001000349:
		areaName = "胜利街道"
	case 1166551862001000356:
		areaName = "十八里店"
	case 1166551862001000379:
		areaName = "张家湾"
	default:
		areaName = strconv.Itoa(int(categoryId))

	}
	return
}
func status(s string) (ret string) {
	switch s {
	case "developing":
		ret = "开发中"
	case "planning":
		ret = "规划中"
	case "rejected":
		ret = "已拒绝"
	case "resolved":
		ret = "已解决"
	case "status_2":
		ret = "已提测"
	case "status_3":
		ret = "测试联调"
	case "status_4":
		ret = "待上线"
	default:
		ret = s
	}
	return
}
func fatherNeed(needId int64, ss []model.M) string {
	for _, s := range ss {
		if needId == s.Ids {
			return s.Name
		}
	}
	return "---"
}

//type table struct {
//	sheet  string
//	axis   string
//	value  interface{}
//	header []string
//	rows interface{}
//}
//
//var tables []table

// ExportXlsx bug,已完成需求,未完成需求
func ExportXlsx(bb []model.Bug, cc []model.M, pp []model.M, fileName string) {
	var (
		sheetC = "已完成需求"
		sheetP = "未完成需求"
		sheetB = "bug"
	)
	xlsx := excelize.NewFile()
	// 创建一个工作表
	index := xlsx.NewSheet(sheetC)
	xlsx.NewSheet(sheetP)
	xlsx.NewSheet(sheetB)
	// 设置工作簿的默认工作表
	xlsx.SetActiveSheet(index)
	// 设置单元格的值
	xlsx.SetCellValue(sheetC, "A1", ``)
	xlsx.SetCellValue(sheetP, "A1", ``)
	xlsx.SetCellValue(sheetB, "A1", ``)

	titleListB := []string{"需求ID", "需求", "验证时间", "状态", "严重程度", "处理人", "创建时间", "环境类型", "接收处理时间", "关闭时间", "花费"}
	titleListC := []string{"父需求ID", "父需求", "需求ID", "需求", "参与人", "创建人", "开始时间", "完成时间", "状态", "区域", "总工时", "上周工时", "本周花费"}
	titleListP := []string{"父需求ID", "父需求", "需求ID", "需求", "参与人", "创建人", "开始时间", "完成时间", "状态", "区域", "总工时", "上周工时", "本周花费"}
	rand.Seed(time.Now().Unix())

	xlsx.SetSheetRow(sheetC, "A1", &titleListC)
	xlsx.SetSheetRow(sheetP, "A1", &titleListP)
	xlsx.SetSheetRow(sheetB, "A1", &titleListB)
	//表头被第一行用了，只能从第二行开始
	j := 2
	k := 2
	l := 2
	for _, vv := range bb {
		xlsx.SetSheetRow(sheetB, "A"+strconv.Itoa(j), &[]interface{}{vv.Ids, vv.Title, vv.VerifyTime.String(), vv.Status,
			vv.Severity, vv.Fixer, vv.Created.String(), vv.CustomFieldOne, vv.InProgressTime.String(), vv.Closed.String(), vv.EffortCompleted})
		j++
	}
	for _, vv := range cc {
		a, _ := strconv.Atoi(vv.ParentId)
		xlsx.SetSheetRow(sheetC, "A"+strconv.Itoa(k), &[]interface{}{vv.ParentId, fatherNeed(int64(a), cc), strconv.Itoa(int(vv.Ids)) + "\n", vv.Name,
			vv.Owner, vv.Creator, vv.CustomFieldOne + "\t", vv.Complete.String() + "\t", status(vv.Status), area(vv.CategoryId), vv.EffortCompleted, vv.Last, vv.ThisEffort})
		k++
	}
	for _, vv := range pp {
		a, _ := strconv.Atoi(vv.ParentId)
		xlsx.SetSheetRow(sheetP, "A"+strconv.Itoa(l), &[]interface{}{vv.ParentId, fatherNeed(int64(a), pp), strconv.Itoa(int(vv.Ids)) + "\n", vv.Name,
			vv.Owner, vv.Creator, vv.CustomFieldOne + "\t", vv.Complete.String() + "\t", status(vv.Status), area(vv.CategoryId), vv.EffortCompleted, vv.Last, vv.ThisEffort})
		l++
	}
	// 设置工作簿的默认工作表
	xlsx.SetActiveSheet(index)
	err := xlsx.SaveAs(fileName)
	if err != nil {
		fmt.Println(err)
	}
}
